#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "Player.h"
#include "Stick.h"
#include "SpriteManager.h"
#include "DrawableSprite.h"
#include "time.h"
#include <cmath>
#include <sstream>
#include <SFML/Audio.hpp>



int main()
{
    srand(time(NULL));
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML 2.0 Pickin' Sticks!");

    //  Load the sound for picking up a stick!
    sf::SoundBuffer buffer;
    if(!buffer.loadFromFile("pickup_sound.wav"))
    {
        return 20;
    }

    sf::Sound sound;
    sound.setBuffer(buffer);

    // load the sound for leveling up!
    sf::SoundBuffer buffer2;
    if(!buffer2.loadFromFile("level_up_sound.wav"))
    {
        return 30;
    }

    sf::Sound level_sound;
    level_sound.setBuffer(buffer2);

    //  define level
    int level = 0;


    //Load Background Image, Set Texture add to Sprite
    sf::Sprite backgroundSprite;
    sf::Image backgroundImage;
    sf::Texture backgroundTexture;

    if(!SpriteManager::getInstance().prepareSprite(backgroundSprite,  //Sprite
                                                   backgroundImage,   //Image
                                                   backgroundTexture, //Texture
                                                   "grass_map.png",   // File
                                                   640 , 480,         // Size
                                                   0.0f , 0.0f))     // Position
    {
        return 1;
    }

    SpriteManager::getInstance().addSprite(backgroundSprite);


    sf::Sprite playerDownFrameOneSprite;
    sf::Image playerDownFrameOneImage;
    sf::Texture playerDownFrameOneTexture;

    if(!SpriteManager::getInstance().prepareSprite( playerDownFrameOneSprite ,
                                                    playerDownFrameOneImage ,
                                                    playerDownFrameOneTexture ,
                                                    "player_down_frame_1.png" ,
                                                    75 , 75 ,
                                                    100.0f , 100.0f))
    {
        return 2;
    }

    // Player Down Frame Two
    sf::Sprite playerDownFrameTwoSprite;
    sf::Image playerDownFrameTwoImage;
    sf::Texture playerDownFrameTwoTexture;

    if(!SpriteManager::getInstance().prepareSprite( playerDownFrameTwoSprite ,
                                                    playerDownFrameTwoImage ,
                                                    playerDownFrameTwoTexture ,
                                                    "player_down_frame_2.png" ,
                                                    75 , 75 ,
                                                    100.0f, 100.0f))
    {
        return 3;
    }

    sf::Image playerUpFrameOneImage;
    sf::Texture playerUpFrameOneTexture;
    sf::Sprite playerUpFrameOneSprite;

    if(!SpriteManager::getInstance().prepareSprite(playerUpFrameOneSprite,
                                                   playerUpFrameOneImage,
                                                   playerUpFrameOneTexture,
                                                   "player_up_frame_1.png",
                                                   75, 75,
                                                   100.0f, 100.0f));

    sf::Image playerLeftFrameOneImage;
    sf::Texture playerLeftFrameOneTexture;
    sf::Sprite playerLeftFrameOneSprite;

    if(!SpriteManager::getInstance().prepareSprite(playerLeftFrameOneSprite,
                                                   playerLeftFrameOneImage,
                                                   playerLeftFrameOneTexture,
                                                   "player_left_frame_1.png",
                                                   75, 75,
                                                   100.0f, 100.0f));

    sf::Image playerRightFrameOneImage;
    sf::Texture playerRightFrameOneTexture;
    sf::Sprite playerRightFrameOneSprite;

    if(!SpriteManager::getInstance().prepareSprite(playerRightFrameOneSprite,
                                                   playerRightFrameOneImage,
                                                   playerRightFrameOneTexture,
                                                   "player_right_frame_1.png",
                                                   75, 75,
                                                   100.0f, 100.0f));


    sf::Image stickImage;
    sf::Texture stickTexture;
    sf::Sprite stickSprite;

    if(!SpriteManager::getInstance().prepareSprite(stickSprite,
                                                   stickImage,
                                                   stickTexture,
                                                   "branch.png",
                                                   75, 75,
                                                   300.0f, 300.0f));
    //sf::Sprite player(playerDownFrameOneSprite);
    Stick stick(stickSprite);

    Player player(playerDownFrameOneSprite);




    window.setFramerateLimit(60);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        //  Level Text
        std::ostringstream level_ss;
        level_ss << level;
        sf::Text level_text("Level:  " + level_ss.str());
        level_text.setPosition(22.0, 30.0);

        //  playing with rectangle bounds -- player
        sf::FloatRect playerRect = player.getSprite().getGlobalBounds();
        playerRect.height = playerRect.height/2;
        playerRect.width = playerRect.width/2;
//        std::ostringstream ss;
//        ss << playerRect.left;
//        std::string playerLeftString = "player left: " + ss.str();
//        sf::Text playerXText(playerLeftString);
//        playerXText.setPosition(200.0, 0.0);
//
//        std::ostringstream ss1;
//        ss1 << playerRect.top;
//        std::string playerTopString = "player top: " + ss1.str();
//        sf::Text playerXTextTop(playerTopString);
//        playerXTextTop.setPosition(400.0, 0.0);

        //  playing with rectangle bounds -- stick
        sf::FloatRect stickRect = stick.getSprite().getGlobalBounds();
        stickRect.width = stickRect.width/2;
        stickRect.height = stickRect.height/2;
//        std::ostringstream ss2;
//        ss2 << stickRect.left;
//        std::string stickLeftString = "stick left: " + ss2.str();
//        sf::Text stickLeftText(stickLeftString);
//        stickLeftText.setPosition(200.0, 50.0);

//        std::ostringstream ss3;
//        ss3 << stickRect.top;
//        std::string stickTopString = "stick top: " + ss3.str();
//        sf::Text stickTopText(stickTopString);
//        stickTopText.setPosition(400.0, 50.0);

        if(playerRect.left >= 0 && playerRect.left + playerRect.width <= 610
           && playerRect.top >= 0 && playerRect.top + playerRect.height <= 450)
        {
                        //input handling
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
                if(playerRect.top + playerRect.height + 2 <= 450)
                {
                    player.moveSprite(Player::South);
                }
                player.getSprite().setTexture(playerDownFrameOneTexture);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
                if(playerRect.left + playerRect.width + 2 <= 610)
                {
                    player.moveSprite(Player::East);
                }
                player.getSprite().setTexture(playerRightFrameOneTexture);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
                if(playerRect.top - 2 >= 0)
                {
                   player.moveSprite(Player::North);
                }
                player.getSprite().setTexture(playerUpFrameOneTexture);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
                if(playerRect.left -2 >= 0)
                {
                    player.moveSprite(Player::West);
                }
                player.getSprite().setTexture(playerLeftFrameOneTexture);
            }

            //this is just to check, won't have this in the final game
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                stick.generateCoordinates();
            }
        }
        if(playerRect.intersects(stickRect))
        {
            stick.generateCoordinates();

            while(playerRect.intersects(stickRect))//FIXXXXXXX!!!!!!
            {
                stick.generateCoordinates();
                stickRect = stick.getSprite().getGlobalBounds();
            }

            player.increaseScore(1);

            if(player.getScore() % 10 == 0)
            {
                level_sound.play();
                level++;
            }
            else
            {
                sound.play();
            }
        }

        std::ostringstream ss4;
        ss4 << player.getScore();
        sf::Text scoreText("Score: " + ss4.str());
        scoreText.setPosition(20.0, 0.0);


        window.clear();
        window.draw(backgroundSprite);
        window.draw(player);
        window.draw(stick);
        window.draw(scoreText);
        //window.draw(playerXText);
        //window.draw(playerXTextTop);
        //window.draw(stickLeftText);
        //window.draw(stickTopText);
        window.draw(level_text);
        window.display();
    }

    return 0;
}
